home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17638 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  63 lines

  1. Path: news.uh.edu!bti!usenet
  2. From: "Sean C. Olson" <olson@rose.rsoc.rockwell.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Templates & friends
  5. Date: Tue, 16 Apr 1996 15:09:32 -0500
  6. Organization: Rockwell Space Operations
  7. Message-ID: <3173FE7C.7B2@rose.rsoc.rockwell.com>
  8. References: <317211BF.41C6@sonytel.be>
  9. NNTP-Posting-Host: rose.rsoc.rockwell.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; SunOS 5.4 sun4m)
  14.  
  15. Philip Rademakers wrote:
  16. > Hi,
  17. > Does anybody know how to declare a friend class that is one of the
  18. > argument classes? I tried the following:
  19. > class X {
  20. > protected:
  21. >   X();
  22. > };
  23. > template<class T>
  24. > class foo {
  25. > private:
  26. >   T* _ptr;
  27. > public:
  28. >   friend class T;
  29. >   foo() { _ptr = new T; }
  30. > };
  31.  
  32. Try instead:
  33.  
  34. template <class T> class foo;
  35.  
  36. template <class T> class X {
  37.   friend class foo<T>;
  38.  
  39.   protected:
  40.      X();
  41. };
  42.  
  43. template <class T> class foo {
  44.    private:
  45.       T* _ptr;
  46.    public:
  47.       foo () { _ptr = new T; }
  48. };
  49.  
  50. Basically, the class template foo must be declared a friend of the
  51. class X. But since foo is a parameterized type, you will want X to be as
  52. well for the friend declaration. (I think)
  53.  
  54. -- 
  55. Sean Olson               (713) 282-3740
  56. Unisys Space Systems     olson@rose.rsoc.rockwell.com
  57. RSOC
  58.